package com.gzw.zhihudemo.activity;

import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.gzw.zhihudemo.R;
import com.gzw.zhihudemo.db.CacheDataBase;
import com.gzw.zhihudemo.fragment.MainFragment;
import com.gzw.zhihudemo.fragment.MenuFragment;
import com.gzw.zhihudemo.fragment.NewsFragment;


public class MainActivity extends BaseActivity {

    public static String curId;
    public static String idString;
    private Toolbar toolbar;
    private MenuFragment menuFragment;
    private FrameLayout fl_content;
    private DrawerLayout mDrawerLayout;
    private boolean isLight;
    private SharedPreferences sp;
    private CacheDataBase dbHelper;
    private FragmentTransaction ft;
    private FragmentManager fm;

    private SwipeRefreshLayout sr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        dbHelper = new CacheDataBase(this, 1);

        isLight = sp.getBoolean("isLight", true);

        initView();
        loadLatest();
    }

    public void loadLatest() {
        /*getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
        .replace(R.id.fl_content, new MainFragment(),"latest").commit();*/


        fm = getSupportFragmentManager();

        NewsFragment newsFragment = (NewsFragment) fm.findFragmentByTag("news");
        MainFragment mainFragmet = (MainFragment) fm.findFragmentByTag("latest");

        ft = fm.beginTransaction();
        if (newsFragment != null && mainFragmet != null) {
            ft.hide(newsFragment).show(mainFragmet);
            //切换首页，加载数据
            mainFragmet.LoadFirst();
        } else {
            ft.add(R.id.fl_content, new MainFragment(), "latest");
        }
        ft.commit();
        curId = "latest";
    }


    private void initView() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(getResources().getColor(isLight ? R.color.light_toolbar : R.color.dark_toolbar));
        toolbar.setTitle("首页");
        setSupportActionBar(toolbar);
        setStatusBarColor(getResources().getColor(isLight ? R.color.light_toolbar : R.color.dark_toolbar));
        sr = (SwipeRefreshLayout) findViewById(R.id.sr);
        sr.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        sr.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                replaceFragment();
                sr.setRefreshing(false);
            }
        });
        fl_content = (FrameLayout) findViewById(R.id.fl_content);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        final ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
                R.string.app_name, R.string.app_name);
        mDrawerLayout.setDrawerListener(drawerToggle);
        drawerToggle.syncState();


    }


    /**
     * 刷新的时候替换一下Fragment
     */
    public void replaceFragment() {
        if (curId.equals("latest")) {
            MainFragment mainFragmet = (MainFragment) getSupportFragmentManager().findFragmentByTag("latest");
            mainFragmet.LoadFirst();
        } else {

        }
    }

    public void setSwipeRefreshEnable(Boolean enable) {

        sr.setEnabled(enable);
    }

    /**
     * 获取主题的模式
     *
     * @return
     */
    public boolean isLight() {
        // TODO Auto-generated method stub
        return isLight;
    }

    /**
     * 关闭侧栏菜单
     */
    public void closeMenu() {
        mDrawerLayout.closeDrawers();

    }

    public void setToolbarTitle(String title) {
        toolbar.setTitle(title);
    }

    public String getCurId() {
        return curId;
    }

    public static void setCurId(String Id) {
        curId = Id;
    }

    public SwipeRefreshLayout getSr() {
        return sr;
    }

    public CacheDataBase getdbHelper() {
        return dbHelper;
    }

    public void setSr(SwipeRefreshLayout sr) {
        this.sr = sr;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        menu.getItem(0).setTitle(sp.getBoolean("isLight", true) ? "夜间模式" : "日间模式");

        return true;
    }

    @TargetApi(21)
    private void setStatusBarColor(int statusBarColor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // If both system bars are black, we can remove these from our layout,
            // removing or shrinking the SurfaceFlinger overlay required for our views.
            Window window = this.getWindow();
            if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            } else {
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            }
            window.setStatusBarColor(statusBarColor);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_mode) {
            isLight = !isLight;
            item.setTitle(isLight ? "夜间模式" : "日间模式");
            toolbar.setBackgroundColor(getResources().getColor(isLight ? R.color.light_toolbar : R.color.dark_toolbar));
            setStatusBarColor(getResources().getColor(isLight ? R.color.light_toolbar : R.color.dark_toolbar));
            if (curId.equals("latest")) {
                ((MainFragment) getSupportFragmentManager().findFragmentByTag("latest")).updateTheme();
                //((NewsFragment)getSupportFragmentManager().findFragmentByTag("news")).updateTheme();
            } else {

                ((MainFragment) getSupportFragmentManager().findFragmentByTag("latest")).updateTheme();
                if (((NewsFragment) getSupportFragmentManager().findFragmentByTag("news")) != null) {
                    ((NewsFragment) getSupportFragmentManager().findFragmentByTag("news")).updateTheme();
                }
            }
            ((MenuFragment) getSupportFragmentManager().findFragmentById(R.id.menu_fragment)).updateTheme();
            sp.edit().putBoolean("isLight", isLight).apply();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        setToolbarTitle("首页");
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawers();
            return;
        } else {

        }
        super.onBackPressed();
    }
}
